const express = require('express')
const router = express.Router()
//导入数据库 sqlFun('sql',[arr],res=>{}) 是函数
const sqlFn = require('./mysql')
//路由接口
 //导入jsonwebtoken 密钥
const jwt = require('jsonwebtoken')
//config.jwtSecert
const config = require('./secert')


//登录接口
/**
 * 语法：
 * 如60，"2 days"，"10h"，"7d"，Expiration time，过期时间
 *  jwt.sign({},'秘钥','过期时间,{expiresIn:20*1,'1day''1h'}')
 */

/**
 * 登录 login
 * 接受的字段：username,password 
 */ 
 router.post('/login', (req, res) => {
    let { username, password } = req.body
    //请求数据库
    let sql = "select * from userlogin where username=? and password=?";
    let arr = [username, password]
    sqlFn(sql, arr, result => {
        if (result.length > 0) {
            let token = jwt.sign({
                username: result[0].username,
                id: result[0].id
            }, config.jwtSecert, {
                expiresIn: 20 * 1
            })
            res.send({
                status: 200,
                data: token
            })
        } else {
            res.send({
                status: 404,
                msg: '信息错误'
            })
        }

    })
})

/**
 * 注册接口 /register
 */
router.post("/register", (req, res) => {
    const {
        username,
        password
    } = req.body;
    const sql = "insert into userinfo values(null,?,?)";
    const arr = [username, password];
    sqlFn(sql, arr, (result) => {
        if (result.affectedRows > 0) {
            res.send({
                msg: "注册成功",
                status: 200
            })
        } else {
            res.status(401).json({
                errors: "用户名密码错误"
            })
        }
    })
})

//学生信息列表 
router.get('/studentList', (req, res) => {
    const page = req.query.page || 1;
    const sqlLen = "select * from studentinfo where id";
    sqlFn(sqlLen, null, data => {
        let len = data.length;
        const sql = "select * from studentinfo order by id desc limit 12 offset " + (page - 1) * 12;
        sqlFn(sql, null, result => {
            if (result.length > 0) {
                res.send({
                    status: 200,
                    data: result,
                    pageSize: 12,
                    total: len
                })
            } else {
                res.send({
                    status: 500,
                    msg: "暂无数据"
                })
            }
        })
    })
})


/**
 * 学生信息查询接口 search
 * 参数：search
 */
 router.get("/studentSearch", (req, res) => {
    var search = req.query.search;
    const sql = "select * from studentinfo where concat(`stuid`,`stuname`) like '%" + search + "%'";
    sqlFn(sql, null, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})


/**
 * 学生添加
 * 参数： stuid, stuname, stusex, stuclass, stufactory, stuphonee
 */
router.get("/addstuinfo", (req, res) => {
    /**
     * 获取参数
     */
    var stuid = req.query.stuid || "";
    var stuname = req.query.stuname || "";
    var stusex = req.query.stusex || "";
    var stuclass = req.query.stuclass || "";
    var stufactory = req.query.stufactory || "";
    var stuphone = req.query.stuphone || "";


    const sql = "insert into studentinfo values (null,'','','','','','','')"
    var arr = [stuid, stuname, stusex, stuclass, stufactory, stuphone];
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "添加成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "添加失败"
            })
        }
    })
})
   





/**
 * 学生信息删除接口 id
 */
router.get("/deletestuinfo", (req, res) => {
    var id = req.query.id;
    const sql = "delete from studentinfo where id=?"
    const arr = [id];
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "删除成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "删除失败"
            })
        }
    })
})



//教师信息管理
//---------------------------------------------------------------


//教师信息列表 
router.get('/teacherList', (req, res) => {
    const page = req.query.page || 1;
    const sqlLen = "select * from teacherinfo where id";
    sqlFn(sqlLen, null, data => {
        let len = data.length;
        const sql = "select * from teacherinfo order by id desc limit 12 offset " + (page - 1) * 12;
        sqlFn(sql, null, result => {
            if (result.length > 0) {
                res.send({
                    status: 200,
                    data: result,
                    pageSize: 12,
                    total: len
                })
            } else {
                res.send({
                    status: 500,
                    msg: "暂无数据"
                })
            }
        })
    })
})


/**
 * 教师信息查询接口 search
 * 参数：search
 */
 router.get("/teacherSearch", (req, res) => {
    var search = req.query.search;
    const sql = "select * from teacherinfo where concat(`tid`,`tname`) like '%" + search + "%'";
    sqlFn(sql, null, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})


/**
 * 教师添加
 * 参数： title cid  category sellPoint price num descs paramsInfo image
 */
router.get("/addtinfo", (req, res) => {
    /**
     * 获取参数
     */
    var tid = req.query.tid || "";
    var tname = req.query.tname || "";
    var tsex = req.query.tsex || "";
    var ttitle = req.query.ttitle || "";
    var tfactory = req.query.tfactory || "";
    var tphone = req.query.tphone || "";


    const sql = "insert into teacherinfo values (null,'','','','','','','')"
    var arr = [tid, tname, tsex, ttitle, tfactory, tphone];
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "添加成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "添加失败"
            })
        }
    })
})
   





/**
 * 教师信息删除接口 id
 */
router.get("/deletetinfo", (req, res) => {
    var id = req.query.id;
    const sql = "delete from teacherinfo where id=?"
    const arr = [id];
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "删除成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "删除失败"
            })
        }
    })
})





//---------------------------------------------------------------

/**
 * 商品列表：获取分页 {total:'',arr:[{},{},{}],pagesize:8,}
 * 参数：page 页码
 */
 router.get('/projectList', (req, res) => {
    const page = req.query.page || 1;
    const sqlLen = "select * from project where id";
    sqlFn(sqlLen, null, data => {
        let len = data.length;
        const sql = "select * from project order by id desc limit 12 offset " + (page - 1) * 12;
        sqlFn(sql, null, result => {
            if (result.length > 0) {
                res.send({
                    status: 200,
                    data: result,
                    pageSize: 12,
                    total: len
                })
            } else {
                res.send({
                    status: 500,
                    msg: "暂无数据"
                })
            }
        })
    })
})

/**
 * 商品查询接口 search
 * 参数：search
 */
router.get("/search", (req, res) => {
    var search = req.query.search;
    const sql = "select * from project where concat(`title`,`price`,`num`) like '%" + search + "%'";
    sqlFn(sql, null, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})


/**
 * 商品添加
 * 参数： title cid  category sellPoint price num descs paramsInfo image
 */
router.get("/backend/item/insertTbItem", (req, res) => {
    /**
     * 获取参数
     */
    var title = req.query.title || "";
    var cid = req.query.cid || "";
    var category = req.query.category || "";
    var sellPoint = req.query.sellPoint || "";
    var price = req.query.price || "";
    var num = req.query.num || "";
    var descs = req.query.descs || "";
    var paramsInfo = req.query.paramsInfo || "";
    var image = req.query.image || "";

    const sql = "insert into project values (null,?,?,?,?,?,?,?,'',1,'','',?,?)"
    var arr = [title, image, sellPoint, price, cid, category, num, descs, paramsInfo];
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "添加成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "添加失败"
            })
        }
    })
})


/**
 * 商品删除接口 id
 */
router.get("/backend/item/deleteItemById", (req, res) => {
    var id = req.query.id;
    const sql = "delete from project where id=?"
    const arr = [id];
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "删除成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "删除失败"
            })
        }
    })
})





/**
 * 修改商品
 */

router.get("/backend/item/updateTbItem", (req, res) => {
    var id = req.query.id;
    var title = req.query.title || "";
    var sellPoint = req.query.sellPoint || "";
    var price = req.query.price || "";
    var cid = req.query.cid || "";
    var category = req.query.category || "";
    var num = req.query.num || "";
    var descs = req.query.descs || "";
    var paramsInfo = req.query.paramsInfo || "";
    var image = req.query.image || "";
    var sql = "update project set title=?,sellPoint=?,price=?,cid=?,category=?,num=?,descs=?,paramsInfo=?,image=? where id=?";
    var arr = [title, sellPoint, price, cid, category, num, descs, paramsInfo, image, id];
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "修改成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "修改失败"
            })
        }
    })
})



module.exports = router